# 参考项目：https://blog.csdn.net/LepoLepo/article/details/84721380
# 忽略 AVX2 指令等
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

from tensorflow.keras.datasets import boston_housing

# 训练数据集 测试数据集
(train_data, train_targets), (test_data, test_targets) = boston_housing.load_data()

# 数据标准化
# 由于该数据集各样本取值范围差异很大，直接将数据输入到神经网络中的话，学习将会变得困难。
# 普遍的解决方法是对每个特征做标准化，即用每个数据的输入特征（输入数据矩阵的列），减去特征平均值(mean)，除以标准差(std)。
# 这样，特征平均值就变成了0，标准差是1。

# 特征平均值（mean）
mean = train_data.mean(axis=0)
# 标准差(std)
std = train_data.std(axis=0)

train_data -= mean
train_data /= std

test_data -= mean
test_data /= std

# 模型定义
# 为样本数量很小，所以我们使用一个很小的网络，他包含2个隐藏层，每层64个单元。
# 我们用较小的网络来降低训练数据过小所带来的过拟合问题。
from tensorflow.keras import models
from tensorflow.keras import layers


def build_model():
    model = models.Sequential()
    model.add(layers.Dense(64, activation='relu', input_shape=(train_data.shape[1],)))
    model.add(layers.Dense(64, activation='relu'))
    model.add(layers.Dense(1))
    # mse损失函数, 预测值与目标值之差的平方，这是回归问题常用的损失函数
    # 监控指标mae, 表示预测值与目标值之差的绝对值
    model.compile(optimizer='rmsprop', loss='mse', metrics=['mae'])
    return model


# K折验证
import numpy as np

k = 4
num_val_samples = len(train_data) // k
num_epochs = 500
all_mae_histories = []

for i in range(k):
    print('processing fold #', i)

    val_data = train_data[i * num_val_samples: (i + 1) * num_val_samples]
    val_targets = train_targets[i * num_val_samples: (i + 1) * num_val_samples]

    partial_train_data = np.concatenate([train_data[: i * num_val_samples],
                                         train_data[(i + 1) * num_val_samples:]], axis=0)
    partial_train_targets = np.concatenate([train_targets[: i * num_val_samples],
                                            train_targets[(i + 1) * num_val_samples:]],
                                           axis=0)

    model = build_model()
    history = model.fit(partial_train_data, partial_train_targets,
                        validation_data=(val_data, val_targets),
                        epochs=num_epochs, batch_size=1, verbose=0)
    # val_mean_absolute_error 报错，可能与版本 key 值有关
    # mae_history = history.history['val_mean_absolute_error']
    mae_history = history.history['val_mae']
    all_mae_histories.append(mae_history)

# 计算所有轮次中K折验证平均值
average_mae_history = [np.mean([x[i] for x in all_mae_histories])
                       for i in range(num_epochs)]


# 绘制曲线
def smooth_curve(points, factor=0.9):
    smoothed_points = []
    for point in points:
        if smoothed_points:
            previous_point = smoothed_points[-1]
            smoothed_points.append(factor * previous_point + (1 - factor) * point)
        else:
            smoothed_points.append(point)
    return smoothed_points


smooth_mae_history = smooth_curve(average_mae_history[10:])

import matplotlib.pyplot as plt

plt.plot(range(1, len(smooth_mae_history) + 1), smooth_mae_history)
plt.xlabel('Epochs')
plt.ylabel('Validation MAE')
plt.show()

prediction = model.predict(test_data)
print("The price that predict:", prediction[77][0])
print("Actual price:", test_targets[77])
